package jaux.tank.generator.util;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class CollectionUtil {

	private static final ObjectMapper objectMapper = new ObjectMapper();

	private CollectionUtil() {
	}

	public static Object[] map(Collection<?> collection, String attrName) {
		if(collection ==null ||collection.isEmpty()) {
			return new Object[0];
		}
		TypeReference<List<Map<String, Object>>> mapLstTypeRefer = new TypeReference<List<Map<String, Object>>>() {};
		return objectMapper.convertValue(collection, mapLstTypeRefer)
				.stream()
				.map(m -> m.get(attrName))
				.toArray(Object[]::new);
	}

}
